package com.example.demo.controller.orderSystem;

import com.alibaba.fastjson.JSON;
import com.example.demo.controller.OrderControllor;
import com.example.demo.entity.AcceptInfo;
import com.example.demo.entity.OrderBasicInfo;
import com.example.demo.entity.SendInfo;
import com.example.demo.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

@Component
public class OrderManage {

    @Resource
    private OrderBasicInfoMapper orderBasicInfoMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private AcceptInfoMapper acceptInfoMapper;

    @Resource
    private SendInfoMapper sendInfoMapper;

    public static OrderManage orderManage;


    @PostConstruct
    public void init(){
        orderManage=this;
    }
    public static String  createNewOrder(OrderControllor.CreateOrder createOrder){
        System.out.println("创建订单ing");
        OrderBasicInfo orderBasicInfo = new OrderBasicInfo();

        String phone = createOrder.getAccept_phone();
//        System.out.println(phone);
        String accept_id = orderManage.userMapper.get_user_id(createOrder.getAccept_phone());
        String sender_id = orderManage.userMapper.get_user_id(createOrder.getSender_phone());
        if(accept_id == null || sender_id == null) return null;
//        orderBasicInfo.setAccept_id(accept_id);
//        orderBasicInfo.setSender_id(sender_id);
//
//        System.out.println(orderBasicInfo.getAccept_id());
//        System.out.println(orderBasicInfo.getSender_id());
//        System.out.println(JSON.toJSONString(orderBasicInfo));
        int now = orderManage.orderMapper.insert_new_order(sender_id,accept_id);
        System.out.println(now);

        String new_order_id = orderManage.orderMapper.find_new_order_id();
        System.out.println(new_order_id);

        AcceptInfo acceptInfo = new AcceptInfo();
        acceptInfo.setOrder_id(new_order_id);
        acceptInfo.setAccept_id(accept_id);
        acceptInfo.setAccept_address(createOrder.getAccept_address());

        orderManage.acceptInfoMapper.insert(acceptInfo);



        SendInfo sendInfo = new SendInfo();
        sendInfo.setOrder_id(new_order_id);
        sendInfo.setSender_id(sender_id);
        sendInfo.setSender_address(createOrder.getSender_address());
        acceptInfo.setAccept_address(createOrder.getAccept_address());

        orderManage.sendInfoMapper.insert(sendInfo);
        return new_order_id;
    }


    public static boolean cancelNewOrder(OrderBasicInfo orderBasicInfo){
        /**
         * 当订单状态为0时，可以取消订单。
         * 操作为查询数
         * 取消成功返回true，否则返回false
         */
        System.out.println(orderBasicInfo.getOrder_id());

        System.out.println(orderBasicInfo.getOrder_status());
        if(orderBasicInfo.getOrder_status() == 0){
            orderManage.orderMapper.delete_order_basic_inf(orderBasicInfo.getOrder_id());
            orderManage.sendInfoMapper.deleteByPrimaryKey(orderBasicInfo.getOrder_id());
            orderManage.acceptInfoMapper.deleteByPrimaryKey(orderBasicInfo.getOrder_id());
            return true;
        }
        return false;
    }
}
